package com.netflix.hystrix.test;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;

/**
 * @Author Austin
 * @Description HystrixCommand HelloWorld
 * @Date 2022/8/15 16:38
 **/
public class HelloHystrixCommand extends HystrixCommand<String> {

    protected HelloHystrixCommand() {
        // super(HystrixCommandGroupKey.Factory.asKey("hello"));
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hello"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        // 开启熔断模式
                        .withCircuitBreakerEnabled(true)
                        // 出现错误的比率超过30%就开启熔断
                        .withCircuitBreakerErrorThresholdPercentage(30)
                        // 至少有10个请求才进行errorThresholdPercentage错误百分比计算
                        .withCircuitBreakerRequestVolumeThreshold(10)
                        // 半开试探休眠时间，这里设置为3秒
                        .withCircuitBreakerSleepWindowInMilliseconds(3000)
                        .withExecutionTimeoutEnabled(true)
                        .withExecutionTimeoutInMilliseconds(600)));

    }

    @Override
    protected String run() throws Exception {
        System.out.println("执行请求 run");
        Thread.sleep(700);
        return "success";
    }

    @Override
    protected String getFallback() {
        System.out.println("执行降级 fallback");
        return "error";
    }
}
